<?php
/**
 * this class defines the validations that can be expected for a valid User object. 
 * This is based on the default context of how the system will be used
 * At any time a different validator can be used depending on the validations requirements
 *
 */
class user_validation_Default extends Validator 
{

	
	public function __construct(user_User $user)
	{
		$this->_user = $user;
		$this->defineValidations();
	}
	
	/**
	 * this method will be in charge of creating the validator chains that will operate
	 * on the properties of this object. It should know how to define the validations of it's child objects as well
	 *
	 */
	private function defineValidations()
	{
		$this->_definedFields['email'] = $this->defineEmail();

		$this->Profile = $this->_user->Profile->getDefaultValidator();
	}
	
	/**
	 * validates that a user object is valid in it's current state
	 *
	 * @param user_User $user
	 */
	public function validate(user_User $user)
	{
		$messages = array();
		
		// validate all our defined properties
		foreach($this->_definedFields as $field => $vchain) {
			if(!$vchain->isValid($user->$field)) {
				$messages = array_merge($messages, $vchain->getMessages());
			}
		}
		

		return $messages;
	}
	
	/**
	 * returns the validator chain for a particular item of this object
	 * 
	 */
	public function getValidatorChain($property)
	{
		return $this->_definedFields[$property];
	}
	
	/**
	 * defines what validator chain to use for an email address
	 *
	 */
	private function defineEmail()
	{
		$validatorChain = new Zend_Validate();
		$validatorChain->addValidator(new Zend_Validate_EmailAddress());
		return $validatorChain;
	}
	
	/**
	 * defines the validator chain for a new email address
	 */
	public function newEmailAddress()
	{
		$validatorChain = new Zend_Validate();
		$validatorChain->addValidator(new validators_UserExists());
		return $validatorChain;
	}
	
	
}
?>